package com.hm.plugin.report.excel;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.hm.plugin.report.model.ReadCell;


public class ImportExcel {

	@SuppressWarnings("unused")
	private Class<?> objClass;

	private Map<String, List<ReadCell>> content;

	private Map<Object, ReadCell> dataTypes;

	public ImportExcel(Class<?> objClass) {
		this.objClass = objClass;
		this.dataTypes = new HashMap<Object, ReadCell>();
	}

	public ImportExcel dataDefine(String key, ReadCell cell) {
		dataTypes.put(key, cell);
		return this;
	}

	private Workbook getWorkBook(InputStream inputStream) throws IOException {
		Workbook workbook = null;
		try {
			workbook = new XSSFWorkbook(inputStream);
		} catch (Exception e) {
			workbook = new HSSFWorkbook(inputStream);
		}
		return workbook;
	}

	public ImportExcel read(InputStream inputStream) throws Exception {
		Workbook xssfWorkbook = getWorkBook(inputStream);
		Sheet sheet = xssfWorkbook.getSheetAt(0);

		sheet.getRow(0).forEach(cell -> {
			String key = cell + "";

			ReadCell readCell = dataTypes.get(key);
			dataTypes.put(cell.getColumnIndex(), readCell);

			dataTypes.remove(key);
		});

		content = new HashMap<String, List<ReadCell>>();
		boolean isTop = true;
		for (Row row : sheet) {
			if (isTop) {
				isTop = false;
			} else {
				List<ReadCell> item = new ArrayList<ReadCell>();
				for (Cell cell : row) {
					ReadCell tmp = dataTypes.get(cell.getColumnIndex());

					ReadCell readCell = new ReadCell();
					{
						readCell.setMethod(tmp.getMethod());
						readCell.setFormat(tmp.getFormat());
						readCell.setExecelType(tmp.getExecelType());
						readCell.setValueType(tmp.getValueType());
						readCell.setValue(cell);
					}
					item.add(readCell);
				}
				content.put(UUID.randomUUID().toString(), item);
			}
		}

		return this;
	}

	/*public List<Object> full() throws Exception {

		List<Object> items = new ArrayList<Object>();

		for (String key : content.keySet()) {
			Class<?> oClass = TestModel.class;
			try {
				Object model = oClass.newInstance();

				for (ReadCell col : content.get(key)) {

					if (null == col.getValue())
						continue;

					Method declaredMethod = oClass.getDeclaredMethod(col.getMethod(), col.getValueType());
					declaredMethod.setAccessible(true);
					if (Integer.class.equals(col.getValueType())) {
						double number = col.getValue().getNumericCellValue();
						declaredMethod.invoke(model, (int) number);
					} else if (Date.class.equals(col.getValueType())) {
						declaredMethod.invoke(model, col.getValue().getDateCellValue());
					} else if (Double.class.equals(col.getValueType())) {
						declaredMethod.invoke(model, col.getValue().getNumericCellValue());
					} else if (Float.class.equals(col.getValueType())) {
						double number = col.getValue().getNumericCellValue();
						declaredMethod.invoke(model, (float) number);
					} else {
						if (null != col.getExecelType() && col.getExecelType().equals(Date.class)) {
							Date cellValue = col.getValue().getDateCellValue();

							declaredMethod.invoke(model, new SimpleDateFormat(col.getFormat()).format(cellValue));
						} else {
							declaredMethod.invoke(model, col.getValue().getStringCellValue());
						}
					}
				}

				items.add(model);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return items;
	}*/
}
